﻿@model IEnumerable<EdexApiClient.Invoices>

@{
    ViewBag.Title = "Документы";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<header class="jumbotron subhead" id="overview">
    <h1></h1>
    <p class="lead">Входящие накладные</p>
</header>

<form>
    <div class="input-append">
        <input type="text" class="input-xxlarge" name="search" value="@Request["search"]" />
        <button class="btn" type="submit"><i class="icon-filter"></i> Фильтр</button>
    </div>
</form>

<table data-name="INVOICES" class="table table-striped">
    <thead>
        <tr>
            <th></th>
            <th>Поставщик</th>
            <th>Склад</th>
            <th>Дата документа</th>
            <th>№ документа</th>
            <th>Сумма</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var record in Model) {
        <tr data-index="@record.GUID_RECORD">
            <td data-type="noclick"><input type="checkbox" name="SELECTED_GUID_RECORD" value="@record.GUID_RECORD" checked="checked" /></td>
            <td>@(record.COMPANY != null ? record.COMPANY.COMPANY_NAME : String.Empty)</td>
            <td>@(record.LOCATION != null ? record.LOCATION.LOCATION_NAME : String.Empty)</td>
            <td>@record.DOC_DATE.ToShortDateString()</td>
            <td>@record.DOC_NUMBER</td>
            <td>@record.TOTAL_AMOUNT</td>
        </tr>
        }
    </tbody>
</table>

<p>
    <a href="#" class="btn" data-action="DOWNLOAD_INVOICES"> Загрузить отмеченные накладные</a>
</p>


<div id="invoice-details"></div>
<div id="invoice-line-details"></div>
<div id="item-mapping"></div>
<div id="location-mapping"></div>


<script type="text/javascript">

    $(function () {

        $("table[data-name='INVOICES'] tr td[data-type!='noclick']").live("click", function () {

            var id = $(this).parent().attr("data-index");

            //$('.modal[data-name="INVOICES"]').modal({ remote: '/document/details/' + id, show: true });

            $("#invoice-details").load('/document/details/' + id, function () {
                $('.modal[data-name="INVOICES"]').modal("show");
            });
        });

        $("table[data-name='INVOICE_LINES'] tr td[data-type!='noclick']").live("click", function () {

            var id = $(this).parent().attr("data-index");

            $("#invoice-line-details").load('/document/linedetails/' + id, function () {
                $('.modal[data-name="INVOICES"]').modal("hide");
                $('.modal[data-name="INVOICE_LINES"]').modal("show");
            });
        });

        $(".btn[data-action='EDIT-ITEM-MAPPING']").live("click", function () {

            var id = $(this).attr("data-index");

            $("#item-mapping").load('/Product/MapDetailsByInvoiceLineEdit/' + id, function () {

                $('.modal[data-name="INVOICE_LINES"]').modal("hide");
                $('.modal[data-name="ITEMS_MAPPING"]').modal("show");
            });
        });

        $(".btn[data-action='EDIT-LOCATION-MAPPING']").live("click", function () {

            var id = $(this).attr("data-index");

            $("#location-mapping").load('/Location/MapDetailsByInvoiceLineEdit/' + id, function () {

                $('.modal[data-name="INVOICES"]').modal("hide");
                $('.modal[data-name="LOCATIONS_MAPPING"]').modal("show");
            });
        });

        $(".btn[data-action='ITEM-FILTER']").live("click", function () {

            var filter = $("input[name='ITEM-FILTER']").val();
            $("select[name='MAP_ITEM_GUID']").load("/product/FindAndReturnHtmlList/" + filter);
        });

        $(".btn[data-action='ITEMS_MAPPING-CLOSE']").live("click", function () {
            $('.modal[data-name="INVOICE_LINES"]').modal("show");
        });

        $(".btn[data-action='ITEMS_MAPPING-SAVE']").live("click", function () {

            var _ITEM_GUID = $("form[name='ITEMS_MAPPING'] select[name='MAP_ITEM_GUID']").val();
            var _SUPPLIER_ITEM_GUID = $("form[name='ITEMS_MAPPING'] input[name='ITEM_GUID']").val();

            $.ajax({
                type: 'POST',
                url: '/product/MapDetailsSave/',
                data: {
                    ITEM_GUID: _ITEM_GUID,
                    SUPPLIER_ITEM_GUID: _SUPPLIER_ITEM_GUID
                },
                success: function () {
                    $('.modal[data-name="ITEMS_MAPPING"]').modal("hide");
                    $('.modal[data-name="INVOICE_LINES"]').modal("show");
                }
            });

        });

        $(".btn[data-action='INVOICE_LINES-CLOSE']").live("click", function () {
            $('.modal[data-name="INVOICES"]').modal("show");
        });

        $(".btn[data-action='LOCATION-FILTER']").live("click", function () {

            var filter = $("input[name='LOCATION-FILTER']").val();
            $("select[name='RIGHT_LOCATION_GUID']").load("/location/FindAndReturnHtmlList/" + filter);
        });

        $(".btn[data-action='LOCATIONS_MAPPING-CLOSE']").live("click", function () {
            $('.modal[data-name="INVOICES"]').modal("show");
        });

        $(".btn[data-action='LOCATIONS_MAPPING-SAVE']").live("click", function () {

            $.ajax({
                type: 'POST',
                url: '/location/MapDetailsSave/',
                data: $("form[name='LOCATIONS_MAPPING']").serialize(),
                success: function () {
                    $('.modal[data-name="LOCATIONS_MAPPING"]').modal("hide");
                    $('.modal[data-name="INVOICES"]').modal("show");
                }
            });

        });


        $(".btn[data-action='DOWNLOAD_INVOICES']").on("click", function () {

            var ids = [];
            var path = "";

            $("input[name='SELECTED_GUID_RECORD']:checked").each(function (i) {
                ids.push[$(this).val()];
                path += "GUID_RECORD=" + $(this).val() + "&" 
            });

            document.location = "/document/download/?" + path;
        });

    });
</script>